package com.bysj.lms.controller;


import com.bysj.lms.common.ResultModel;
import com.bysj.lms.pojo.entity.Permission;
import com.bysj.lms.pojo.vo.PermissionVo;
import com.bysj.lms.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-02-04
 */
//@Api(tags = "权限管理")
//@RestController
//@RequestMapping("/permission")
public class PermissionController {

    private final Logger logger = LoggerFactory.getLogger(PermissionController.class);

    @Autowired
    IPermissionService permissionService;

    @ApiOperation(value = "添加权限")
    @PostMapping("/insert")
    public ResultModel insert(PermissionVo permissionVo){
        logger.info("权限信息:"+permissionVo);
        boolean save = permissionService.save(new Permission(permissionVo));
        if(save){
            return ResultModel.success();
        }else{
            return ResultModel.failure();
        }
    }


    @ApiOperation(value = "查找权限",notes = "name为空查询所有权限，不为空模糊查询")
    @GetMapping("/find")
    public ResultModel findByPage(String title,Integer page,Integer count){
        PageInfo pageInfo = permissionService.findByPage(title, page, count);
        return ResultModel.success("权限查询",pageInfo);
    }

    @ApiOperation(value = "根据id编辑权限信息")
    @PutMapping("/update")
    public ResultModel update(Permission permission){
        boolean update = permissionService.updateById(permission);
        if(update){
            return ResultModel.success("编辑成功");
        }
        return ResultModel.failure("编辑失败");
    }

    @ApiOperation(value = "根据id删除权限")
    @PutMapping("/delete")
    public ResultModel delete(Integer id){
        boolean delete = permissionService.removeById(id);
        if(delete){
            return ResultModel.success("删除成功");
        }
        return ResultModel.failure("删除失败");
    }
}

